package com.source.baseData.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.source.baseData.vo.EntrustDefaultInfoVO;
import com.source.baseData.vo.EntrustVO;
import com.source.baseData.entity.Entrust;
import com.source.baseData.service.EntrustService;
import com.source.utils.CommonResult;
import com.source.utils.ResultEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Feng
 * @since 2021-02-12
 */
@Api(tags = "委托基础数据管理")
@RestController
@RequestMapping("/baseData/entrust")
public class EntrustController {

    @Resource
    private EntrustService entrustService;

    @ApiOperation("获取所有委托")
    @GetMapping
    public CommonResult<List<EntrustVO>> find() {
        List<EntrustVO> list = entrustService.getList();
        return new CommonResult<>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation("获取所有委托 并分页")
    @GetMapping("/page")
    public CommonResult<Page<EntrustVO>> find(@RequestParam(defaultValue = "1") Integer current,
                                              @RequestParam(defaultValue = "10") Integer size) {
        Page<EntrustVO> page = entrustService.getPage(current, size);
        return new CommonResult<>(ResultEnum.SUCCESS, page);
    }


    @ApiOperation("委托")
    @PostMapping("/add")
    public CommonResult<String> create(@RequestBody @Validated Entrust entrust) {
        entrust.setStatus(1);
        boolean res = entrustService.save(entrust);
        if (res) {
            return new CommonResult<>(ResultEnum.SUCCESS);
        } else {
            return new CommonResult<>(ResultEnum.ERROR);
        }
    }

    @ApiOperation(value = "取消委托 / 删除被拒绝委托", notes = "- 限制：当前状态为 1.等待 2.拒单")
    @DeleteMapping("/cancel")
    public CommonResult<String> update(String id) {
        if (StringUtils.isEmpty(id)) {
            return new CommonResult<>(ResultEnum.PARAMS_NULL);
        }
        try {
            if (entrustService.cancel(id)) {
                return new CommonResult<>(ResultEnum.SUCCESS);
            } else {
                return new CommonResult<>(ResultEnum.ERROR, "该委托已被接单，无法撤销");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<>(ResultEnum.ERROR);
        }
    }

    //    @ApiOperation(value = "委托二维码（屠宰档口主查看）")
//    @RequestMapping(value = "/viewQRCode", method = RequestMethod.POST)
//    public MyResult viewQRCode(String entrustId, String barcodeMachineNumber) {
//        String res = entrustService.viewQRCode(entrustId, barcodeMachineNumber);
//        if ("error".equals(res)) {
//            return MyResult.build(0, "屠宰档口未接受该委托，查看失败。");
//        } else {
//            return MyResult.build(0, "成功", res);
//        }
//    }

    @ApiOperation("档口接单")
    @PostMapping("/accept")
    public CommonResult<String> accept(String id) {
        String res = entrustService.agree(id);
        if ("error".equals(res)) {
            return new CommonResult<>(ResultEnum.ERROR, "该委托不处于等待接单状态中，接单失败");
        } else {
            return new CommonResult<>(ResultEnum.SUCCESS, res);
        }
    }

    @ApiOperation("档口拒单")
    @PostMapping("/refuse")
    public CommonResult<String> refuse(String id) {
        try {
            if (entrustService.refuse(id)) {
                return new CommonResult<>(ResultEnum.SUCCESS);
            } else {
                return new CommonResult<>(ResultEnum.ERROR, "该委托不处于等待接单状态中，拒单失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<String>(ResultEnum.ERROR);
        }
    }

    @ApiOperation(value = "屠宰档口主开始贴标")
    @PostMapping("/recording")
    public CommonResult<String> recording(@RequestParam(value = "entrustId") String entrustId,
                                          @RequestParam(value = "barcodeMachineNumber") String barcodeMachineNumber) {
        try {
            if (entrustService.recording(entrustId, barcodeMachineNumber)) {
                return new CommonResult<String>(ResultEnum.SUCCESS);
            } else {
                return new CommonResult<String>(ResultEnum.ERROR, "屠宰档口未接受该委托，查看失败。");
            }
        } catch (Exception e) {
            return new CommonResult<String>(ResultEnum.ERROR);
        }
    }

    @ApiOperation(value = "屠宰档口确认完成订单")
    @PostMapping(value = "/finish")
    public CommonResult<String> finish(String id) {
        try {
            if (entrustService.finish(id)) {
//                return MyResult.build(0, "成功");
                return new CommonResult<String>(ResultEnum.SUCCESS);
            } else {
//                return MyResult.build(0, "非贴标中委托，失败。");
                return new CommonResult<String>(ResultEnum.ERROR, "非贴标中委托，失败。");
            }
        } catch (Exception e) {
            return new CommonResult<String>(ResultEnum.ERROR);
        }

    }

    @ApiOperation("根据id查")
    @GetMapping("/get-by-id")
    public CommonResult<EntrustVO> findById(String id) {
        if (StringUtils.isEmpty(id)) {
            return new CommonResult<>(ResultEnum.PARAMS_NULL);
        }
        EntrustVO entrust = entrustService.getByEntrustId(id);
        return new CommonResult<>(ResultEnum.SUCCESS, entrust);
    }

    @ApiOperation("根据根据去向市场/档口查询等待中委托")
    @GetMapping("/list-to-market-waiting")
    public CommonResult<List<EntrustVO>> ToMarketWaiting(String toMarketId) {
        if (StringUtils.isEmpty(toMarketId)) {
            return new CommonResult<>(ResultEnum.PARAMS_NULL);
        }
        List<EntrustVO> list = entrustService.getListByToMarketIdAndStatus(toMarketId, 1);
        if (list.isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation("根据根据去向市场/档口查询等待中委托 - 分页")
    @GetMapping("/page-to-market-waiting")
    public CommonResult<Page<EntrustVO>> ToMarketWaiting(String toMarketId, @RequestParam(value = "current", defaultValue = "1") Integer current,
                                                         @RequestParam(value = "size", defaultValue = "10") Integer size) {
        if (StringUtils.isEmpty(toMarketId)) {
            return new CommonResult<>(ResultEnum.PARAMS_NULL);
        }
        Page<EntrustVO> page = entrustService.getPageByToMarketIdAndStatus(toMarketId, 1, current, size);
        if (page.getRecords().isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, page);
    }

    @ApiOperation("根据根据去向市场/档口查询已接单委托")
    @GetMapping("/list-to-market-agree")
    public CommonResult<List<EntrustVO>> ToMarketAgree(String toMarketId) {
        if (StringUtils.isEmpty(toMarketId)) {
            return new CommonResult<>(ResultEnum.PARAMS_NULL);
        }
        List<EntrustVO> list = entrustService.getListByToMarketIdAndStatus(toMarketId, 2);
        if (list.isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation("根据根据去向市场/档口查询已接单委托 - 分页")
    @GetMapping("page-to-market-agree")
    public CommonResult<Page<EntrustVO>> ToMarketAgree(String toMarketId,
                                                       @RequestParam(value = "current", defaultValue = "1") Integer current,
                                                       @RequestParam(value = "size", defaultValue = "10") Integer size) {
        if (StringUtils.isEmpty(toMarketId)) {
            return new CommonResult<>(ResultEnum.PARAMS_NULL);
        }
        Page<EntrustVO> page = entrustService.getPageByToMarketIdAndStatus(toMarketId, 2, current, size);
        if (page.getRecords().isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, page);
    }

    @ApiOperation("根据去向市场/档口查询已拒单委托")
    @GetMapping("list-to-market-refuse")
    public CommonResult<List<EntrustVO>> ToMarketRefuse(String toMarketId) {
        if (StringUtils.isEmpty(toMarketId)) {
            return new CommonResult<>(ResultEnum.PARAMS_NULL);
        }
        List<EntrustVO> list = entrustService.getListByToMarketIdAndStatus(toMarketId, 3);
        if (list.isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation("根据去向市场/档口查询已拒单委托 - 分页")
    @GetMapping("page-to-market-refuse")
    public CommonResult<Page<EntrustVO>> ToMarketRefuse(String toMarketId, @RequestParam(value = "current", defaultValue = "1") Integer current,
                                                        @RequestParam(value = "size", defaultValue = "10") Integer size) {
        if (StringUtils.isEmpty(toMarketId)) {
            return new CommonResult<>(ResultEnum.PARAMS_NULL);
        }
        Page<EntrustVO> page = entrustService.getPageByToMarketIdAndStatus(toMarketId, 3, current, size);
        if (page.getRecords().isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, page);
    }

    @ApiOperation("根据用户查询等待中委托")
    @GetMapping("list-user-waiting")
    public CommonResult<List<EntrustVO>> userWaiting(String userId) {
        if (StringUtils.isEmpty(userId)) {
            return new CommonResult<>(ResultEnum.PARAMS_NULL);
        }
        List<EntrustVO> list = entrustService.getListByUserIdAndStatus(userId, 1);
        if (list.isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation("根据用户查询等待中委托 - 分页")
    @GetMapping("page-user-waiting")
    public CommonResult<Page<EntrustVO>> userWaiting(String userId, @RequestParam(value = "current", defaultValue = "1") Integer current,
                                                     @RequestParam(value = "size", defaultValue = "10") Integer size) {
        if (StringUtils.isEmpty(userId)) {
            return new CommonResult<>(ResultEnum.PARAMS_NULL);
        }
        Page<EntrustVO> page = entrustService.getPageByUserIdAndStatus(userId, 1, current, size);
        if (page.getRecords().isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, page);
    }

    @ApiOperation("根据用户查询已接单委托")
    @GetMapping("/list-user-agree")
    public CommonResult<List<EntrustVO>> userAgree(String userId) {
        if (StringUtils.isEmpty(userId)) {
            return new CommonResult<>(ResultEnum.PARAMS_NULL);
        }
        List<EntrustVO> list = entrustService.getListByUserIdAndStatus(userId, 2);
        if (list.isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation("根据用户查询已接单委托 - 分页")
    @GetMapping("page-user-agree")
    public CommonResult<Page<EntrustVO>> userAgree(String userId, @RequestParam(defaultValue = "1") Integer current,
                                                   @RequestParam(defaultValue = "10") Integer size) {
        if (StringUtils.isEmpty(userId)) {
            return new CommonResult<>(ResultEnum.PARAMS_NULL);
        }
        Page<EntrustVO> page = entrustService.getPageByUserIdAndStatus(userId, 2, current, size);
        if (page.getRecords().isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, page);
    }

    @ApiOperation("根据用户查询已拒单委托")
    @GetMapping("/list-user-refuse")
    public CommonResult<List<EntrustVO>> userRefuse(String userId) {
        if (StringUtils.isEmpty(userId)) {
            return new CommonResult<>(ResultEnum.PARAMS_NULL);
        }
        List<EntrustVO> list = entrustService.getListByUserIdAndStatus(userId, 3);
        if (list.isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation("根据用户查询已拒单委托 - 分页")
    @GetMapping("page-user-refuse")
    public CommonResult<Page<EntrustVO>> userRefuse(String userId, @RequestParam(value = "current", defaultValue = "1") Integer current,
                                                    @RequestParam(value = "size", defaultValue = "10") Integer size) {
        if (StringUtils.isEmpty(userId)) {
            return new CommonResult<>(ResultEnum.PARAMS_NULL);
        }
        Page<EntrustVO> page = entrustService.getPageByUserIdAndStatus(userId, 3, current, size);
        if (page.getRecords().isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, page);
    }

    @ApiOperation("根据去向市场/档口查询所有委托")
    @GetMapping("/list-to-market")
    public CommonResult<List<EntrustVO>> ToMarketEntrust(String toMarketId) {
        if (StringUtils.isEmpty(toMarketId)) {
            return new CommonResult<>(ResultEnum.PARAMS_NULL);
        }
        List<EntrustVO> list = entrustService.getListByToMarketId(toMarketId);
        if (list.isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation("根据去向市场/档口查询所有委托 - 分页")
    @GetMapping("page-to-market")
    public CommonResult<Page<EntrustVO>> ToMarketEntrust(String toMarketId, @RequestParam(value = "current", defaultValue = "1") Integer current,
                                                         @RequestParam(value = "size", defaultValue = "10") Integer size) {
        if (StringUtils.isEmpty(toMarketId)) {
            return new CommonResult<>(ResultEnum.PARAMS_NULL);
        }
        Page<EntrustVO> page = entrustService.getPageByToMarketId(toMarketId, current, size);
        if (page.getRecords().isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, page);
    }

    @ApiOperation("根据用户查询所有委托")
    @GetMapping("/list-user")
    public CommonResult<List<EntrustVO>> userEntrust(String userId) {
        if (StringUtils.isEmpty(userId)) {
            return new CommonResult<>(ResultEnum.PARAMS_NULL);
        }
        List<EntrustVO> list = entrustService.getListByUserId(userId);
        if (list.isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation("根据用户查询所有委托 - 分页")
    @GetMapping("page-user")
    public CommonResult<Page<EntrustVO>> userEntrust(String userId, @RequestParam(value = "current", defaultValue = "1") Integer current,
                                                     @RequestParam(value = "size", defaultValue = "10") Integer size) {
        if (StringUtils.isEmpty(userId)) {
            return new CommonResult<>(ResultEnum.PARAMS_NULL);
        }
        Page<EntrustVO> page = entrustService.getPageByUserId(userId, current, size);
        if (page.getRecords().isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, page);
    }

    @ApiOperation("多字段模糊查询")
    @GetMapping("list-like")
    public CommonResult<List<EntrustVO>> like(String word) {
        if (StringUtils.isEmpty(word)) {
            return new CommonResult<>(ResultEnum.PARAMS_NULL);
        }
        List<EntrustVO> list = entrustService.like(word);
        if (list.isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation("多字段模糊查询 -分页")
    @GetMapping("page-like")
    public CommonResult<Page<EntrustVO>> like(String word, @RequestParam(value = "current", defaultValue = "1") Integer current,
                                              @RequestParam(value = "size", defaultValue = "10") Integer size) {
        if (StringUtils.isEmpty(word)) {
            return new CommonResult<>(ResultEnum.PARAMS_NULL);
        }
        Page<EntrustVO> page = entrustService.like(word, current, size);
        if (page.getRecords().isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, page);
    }

    @ApiOperation(value = "‘选择委托档口’的默认填写信息")
    @GetMapping(value = "/defaultInfo")
    public CommonResult<EntrustDefaultInfoVO> defaultInfo(@RequestParam(value = "userId") String userId) {
//        return MyResult.build(0, "成功", entrustMapper.defaultInfo(userId));
        try {
            EntrustDefaultInfoVO evo = entrustService.defaultInfo(userId);
            return new CommonResult<EntrustDefaultInfoVO>(ResultEnum.SUCCESS, evo);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<>(ResultEnum.ERROR);
        }
    }

    @ApiOperation(value = "客户分页查询当天上市的委托", notes = "- state的值：\n" +
            "空：所有委托\n" +
            "1：等待中委托\n" +
            "2：已接单委托\n" +
            "3：已拒单委托\n" +
            "4：贴标中委托\n" +
            "5：已完成委托\n" +
            "6：已失效委托")
    @GetMapping(value = "/userEntrust/goMarketToday")
    public CommonResult<IPage<Entrust>> goMarketTodayByUser(@RequestParam(value = "userId") String userId,
                                                            @RequestParam(value = "status", required = false) Integer status,
                                                            @RequestParam(value = "current", defaultValue = "1") Integer current,
                                                            @RequestParam(value = "size", defaultValue = "10") Integer size) {
        try {
            if (status == null) {
                return new CommonResult<IPage<Entrust>>(ResultEnum.SUCCESS, entrustService.findGoMarketTodayByUser(userId, current, size));

            }
            return new CommonResult<IPage<Entrust>>(ResultEnum.SUCCESS, entrustService.findGoMarketTodayByUser(userId, status, current, size));
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<>(ResultEnum.ERROR);
        }
    }

    @ApiOperation(value = "根据去向市场id分页查询当天上市的委托", notes = "- state的值：\n" +
            "空：所有委托\n" +
            "1：等待中委托\n" +
            "2：已接单委托\n" +
            "3：已拒单委托\n" +
            "4：贴标中委托\n" +
            "5：已完成委托\n" +
            "6：已失效委托")
    @GetMapping(value = "/toMarketEntrust/goMarketToday")
    public CommonResult<IPage<Entrust>> goMarketTodayByToMarket(@RequestParam(value = "toMarketId") String toMarketId,
                                                                @RequestParam(value = "status", required = false) Integer status,
                                                                @RequestParam(value = "current", defaultValue = "1") Integer current,
                                                                @RequestParam(value = "size", defaultValue = "10") Integer size) {
        try {
            if (status == null) {
                return new CommonResult<IPage<Entrust>>(ResultEnum.SUCCESS, entrustService.findGoMarketTodayByToMarket(toMarketId, current, size));

            }
            return new CommonResult<IPage<Entrust>>(ResultEnum.SUCCESS, entrustService.findGoMarketTodayByToMarket(toMarketId, status, current, size));
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<>(ResultEnum.ERROR);
        }
    }

    @ApiOperation(value = "根据屠宰档口查询当天上市的委托", notes = "- state的值：\n" +
            "空：所有委托\n" +
            "1：等待中委托\n" +
            "2：已接单委托\n" +
            "3：已拒单委托\n" +
            "4：贴标中委托\n" +
            "5：已完成委托\n" +
            "6：已失效委托")
    @GetMapping(value = "/stallsEntrust/goMarketToday")
    public CommonResult<IPage<Entrust>> goMarketTodayByStalls(@RequestParam(value = "stallsId") String stallsId,
                                                              @RequestParam(value = "status", required = false) Integer status,
                                                              @RequestParam(value = "current", defaultValue = "1") Integer current,
                                                              @RequestParam(value = "size", defaultValue = "10") Integer size) {
        try {
            if (status == null) {
                return new CommonResult<IPage<Entrust>>(ResultEnum.SUCCESS, entrustService.findGoMarketTodayByStalls(stallsId, current, size));

            }
            return new CommonResult<IPage<Entrust>>(ResultEnum.SUCCESS, entrustService.findGoMarketTodayByStalls(stallsId, status, current, size));
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<>(ResultEnum.ERROR);
        }
    }

    @ApiOperation(value = "再次贴标，返回委托二维码")
    @PostMapping(value = "/reLabel")
    public CommonResult<String> reLabel(String entrustId, String barcodeMachineNumber) {

        try {
            String res = entrustService.reLabel(entrustId, barcodeMachineNumber);
            if ("not finish".equals(res)) {
                return new CommonResult<String>(ResultEnum.ERROR, "非已完成订单，不可重新贴标");
            } else {
                return new CommonResult<String>(ResultEnum.SUCCESS, res);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<String>(ResultEnum.ERROR);
        }
    }

    @ApiOperation(value = "客户分页查看历史订单", notes = "- state的值：\n" +
            "空：所有委托\n" +
            "1：等待中委托\n" +
            "2：已接单委托\n" +
            "3：已拒单委托\n" +
            "4：贴标中委托\n" +
            "5：已完成委托\n" +
            "6：已失效委托\n" +
            "- 日期格式：yyyy-MM-dd\n" +
            "- 日期区间：[from, to)")
    @RequestMapping(value = "/userHistory", method = RequestMethod.GET)
    public CommonResult<IPage<Entrust>> userHistory(@RequestParam("userId") String userId,
                                                    @RequestParam(value = "status", required = false) Integer status,
                                                    @RequestParam(value = "current", defaultValue = "1") Integer current,
                                                    @RequestParam(value = "size", defaultValue = "10") Integer size,
                                                    @RequestParam(value = "from") Date from,
                                                    @RequestParam(value = "to") Date to) {
        try {
            if (status == null) {
                return new CommonResult<IPage<Entrust>>(ResultEnum.SUCCESS, entrustService.historyByUser(userId, current, size, from, to));
            }
            return new CommonResult<IPage<Entrust>>(ResultEnum.SUCCESS, entrustService.historyByUserAndStatus(userId, status, current, size, from, to));

        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<>(ResultEnum.ERROR);
        }
    }

    @ApiOperation(value = "去向市场分页查看历史订单", notes = "- state的值：\n" +
            "空：所有委托\n" +
            "1：等待中委托\n" +
            "2：已接单委托\n" +
            "3：已拒单委托\n" +
            "4：贴标中委托\n" +
            "5：已完成委托\n" +
            "6：已失效委托\n" +
            "- 日期格式：yyyy-MM-dd\n" +
            "- 日期区间：[from, to)")
    @GetMapping(value = "/toMarketHistory")
    public CommonResult<IPage<Entrust>> toMarketHistory(@RequestParam("toMarketId") String toMarketId,
                                                        @RequestParam(value = "status", required = false) Integer status,
                                                        @RequestParam(value = "current", defaultValue = "1") Integer current,
                                                        @RequestParam(value = "size", defaultValue = "10") Integer size,
                                                        @RequestParam(value = "from") Date from,
                                                        @RequestParam(value = "to") Date to) {
        try {
            if (status == null) {
                return new CommonResult<IPage<Entrust>>(ResultEnum.SUCCESS, entrustService.historyByToMarket(toMarketId, current, size, from, to));
            }
            return new CommonResult<IPage<Entrust>>(ResultEnum.SUCCESS, entrustService.historyByToMarketAndState(toMarketId, status, current, size, from, to));

        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<>(ResultEnum.ERROR);
        }
    }

    @ApiOperation(value = "屠宰档口分页查看历史订单", notes = "- state的值：\n" +
            "空：所有委托\n" +
            "1：等待中委托\n" +
            "2：已接单委托\n" +
            "3：已拒单委托\n" +
            "4：贴标中委托\n" +
            "5：已完成委托\n" +
            "6：已失效委托\n" +
            "- 日期格式：yyyy-MM-dd\n" +
            "- 日期区间：[from, to)")
    @GetMapping(value = "/stallsHistory")
    public CommonResult<IPage<Entrust>> stallsHistory(@RequestParam("stallsId") String stallsId,
                                                      @RequestParam(value = "status", required = false) Integer status,
                                                      @RequestParam(value = "current", defaultValue = "1") Integer current,
                                                      @RequestParam(value = "size", defaultValue = "10") Integer size,
                                                      @RequestParam(value = "from") Date from,
                                                      @RequestParam(value = "to") Date to) {
        try {
            if (status == null) {
                return new CommonResult<IPage<Entrust>>(ResultEnum.SUCCESS, entrustService.historyByStalls(stallsId, current, size, from, to));
            }
            return new CommonResult<IPage<Entrust>>(ResultEnum.SUCCESS, entrustService.historyByStallsAndState(stallsId, status, current, size, from, to));

        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<>(ResultEnum.ERROR);
        }
    }

    @ApiOperation(value = "委托二维码（屠宰档口主查看）")
    @PostMapping(value = "/viewQRCode")
    public CommonResult<String> viewQRCode(String entrustId, String barcodeMachineNumber) {
        try {
            String res = entrustService.viewQRCode(entrustId, barcodeMachineNumber);
            if ("error".equals(res)) {
//                return MyResult.build(0, "屠宰档口未接受该委托，查看失败。");
                return new CommonResult<String>(ResultEnum.ERROR, "屠宰档口未接受该委托，查看失败。");
            } else {
//                return MyResult.build(0, "成功", res);
                return new CommonResult<String>(ResultEnum.SUCCESS, res);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<String>(ResultEnum.ERROR);
        }
    }


}

